define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'scanwork/custom_field/index' + location.search,
                    add_url: 'scanwork/custom_field/add',
                    edit_url: 'scanwork/custom_field/edit',
                    del_url: 'scanwork/custom_field/del',
                    multi_url: 'scanwork/custom_field/multi',
                    table: 'scanwork_custom_field',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'sort',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: 'ID', width: 60},
                        {field: 'industry_code', title: '行业代码', width: 120, formatter: function(value, row) {
                            var colors = {
                                'furniture_sofa': 'primary',
                                'garment': 'success',
                                'machinery': 'warning'
                            };
                            var color = colors[value] || 'default';
                            return '<span class="label label-' + color + '">' + value + '</span>';
                        }},
                        {field: 'module_name', title: '所属模块', width: 100, searchList: {
                            'order': '订单',
                            'product': '产品',
                            'process': '工序',
                            'resource': '资源',
                            'equipment': '设备',
                            'employee': '员工'
                        }, formatter: Table.api.formatter.normal},
                        {field: 'field_name', title: '字段名称', width: 120},
                        {field: 'field_label', title: '字段标签', width: 120},
                        {field: 'field_type', title: '字段类型', width: 100, searchList: {
                            'text': '文本',
                            'number': '数字',
                            'date': '日期',
                            'datetime': '日期时间',
                            'select': '下拉选择',
                            'textarea': '多行文本',
                            'checkbox': '复选框',
                            'radio': '单选框'
                        }, formatter: Table.api.formatter.normal},
                        {field: 'default_value', title: '默认值', width: 100},
                        {field: 'is_required', title: '必填', width: 60, searchList: {0: '否', 1: '是'}, formatter: function(value) {
                            return value == 1 ? '<span class="label label-danger">必填</span>' : '<span class="label label-default">可选</span>';
                        }},
                        {field: 'is_visible', title: '显示', width: 60, searchList: {0: '否', 1: '是'}, formatter: Table.api.formatter.toggle},
                        {field: 'sort', title: '排序', width: 60},
                        {field: 'status', title: '状态', width: 80, searchList: {normal: '正常', hidden: '隐藏'}, formatter: Table.api.formatter.status},
                        {field: 'operate', title: '操作', width: 150, table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
            
            // 字段类型切换逻辑
            $('#field_type').on('change', function() {
                var val = $(this).val();
                if (val == 'select' || val == 'checkbox' || val == 'radio') {
                    $('#options_group').show();
                } else {
                    $('#options_group').hide();
                }
            });
        },
        edit: function () {
            Controller.api.bindevent();
            
            // 字段类型切换逻辑
            $('#field_type').on('change', function() {
                var val = $(this).val();
                if (val == 'select' || val == 'checkbox' || val == 'radio') {
                    $('#options_group').show();
                } else {
                    $('#options_group').hide();
                }
            });
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});

